package test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class FairLockExample {
    private Lock lock = new ReentrantLock(true); // 创建公平锁

    public void performLockedOperation() {
        lock.lock(); // 获取锁
        try {
            // 被保护的代码逻辑
            System.out.println("Locked and performing operation...");
        } finally {
            lock.unlock(); // 释放锁
        }
    }

    public static void main(String[] args) {
        FairLockExample example = new FairLockExample();
        for (int i = 0; i < 5; i++) {
            new Thread(example::performLockedOperation).start();
        }
    }
}